package com.yb.guigu.pick.security.imports;

import com.yb.guigu.pick.security.annotation.EnableSecurityServer;
import com.yb.guigu.pick.security.config.AppConfig;
import com.yb.guigu.pick.security.config.AuthServerConfig;
import com.yb.guigu.pick.security.config.ResourceServerConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.support.*;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;


/**
 * Copyright (C), 2022-2023, 姚兵
 * Author: 32210
 * Date: 2023/6/1 8:05
 * FileName: GuiguImport
 * Description:
 */


@Slf4j
public class GuiguSecurityImportBeanDefinition implements ImportBeanDefinitionRegistrar {
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) {

        AnnotationAttributes annotationAttributes = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(EnableSecurityServer.class.getName()));
        boolean needLogin = annotationAttributes.getBoolean("needLogin");
        log.info("needLogin:{}",needLogin);
        RootBeanDefinition appBean = new RootBeanDefinition(AppConfig.class);
        registry.registerBeanDefinition(importBeanNameGenerator.generateBeanName(appBean,registry),appBean);

        if(needLogin){
            RootBeanDefinition authConfig = new RootBeanDefinition(AuthServerConfig.class);
            registry.registerBeanDefinition(importBeanNameGenerator.generateBeanName(authConfig,registry),authConfig);

        }else{
            RootBeanDefinition authConfig=new RootBeanDefinition(ResourceServerConfig.class);
            registry.registerBeanDefinition(importBeanNameGenerator.generateBeanName(authConfig,registry),authConfig);
        }

    }
}
